package map.android.baidu.carpool.utils;

import com.baidu.mapframework.api.ComAPIManager;
import com.baidu.mapframework.component.comcore.message.ComParams;
import com.baidu.platform.comapi.util.JsonBuilder;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by wangxin25 on 2015/3/4
 */
public class UniversalSourceFromParameter {

    /*
       来源统计（三个参数）：
          参数： @resid      通过 ComAPIManager.getComAPIManager().getSystemAPI().getResId();
          参数： @qid        基线会通过key值qid，传入值，如果通过组件内部列表页跳转及翻页，需要更新qid
          参数： @src_from   基线会通过key值ldata传入json字符串，eg,{"src_from": "nearby_rec","ext1": "111"},
                            从中解析出@src_from的值
     */

    private static final String TAG = "usfp";

    public static final String KEY_PARAM = "param"; //open api 会通过此参数传入src_from
    public static final String KEY_LDATA = "ldata";
    public static final String KEY_QID = "qid";
    public static final String KEY_SRC_FROM = "src_from";
    public static final String KEY_RES_ID = "resid";

    private static UniversalSourceFromParameter mInstance;

    private String mQid = "";
    private String mSrcFrom = "";
    private String mLdata;

    private UniversalSourceFromParameter(){}

    public static UniversalSourceFromParameter getInstance() {
        if (null == mInstance) {
            mInstance = new UniversalSourceFromParameter();
        }

        return mInstance;
    }

    public void init(ComParams params) {
        clear();

        LogUtils.d(TAG, "init()");

        if (params.containsParameter(UniversalSourceFromParameter.KEY_LDATA)) {
            LogUtils.d(TAG, "init() ldata = " + ((String) params.getBaseParameter(UniversalSourceFromParameter.KEY_LDATA)));
            setLdata((String) params.getBaseParameter(UniversalSourceFromParameter.KEY_LDATA));
        } else if (params.containsParameter(UniversalSourceFromParameter.KEY_PARAM)) {
            LogUtils.d(TAG, "init() params ldata = " + ((String) params.getBaseParameter(UniversalSourceFromParameter.KEY_PARAM)));
            setLdata(createLdata(params));
        }

        if (params.containsParameter(UniversalSourceFromParameter.KEY_QID)) {
            LogUtils.d(TAG, "init() qid = " + ((String) params.getBaseParameter(UniversalSourceFromParameter.KEY_QID)));
            setQid((String) params.getBaseParameter(UniversalSourceFromParameter.KEY_QID));
        }

        LogUtils.d(TAG, "init() parameters = " + toString());
    }

    private String createLdata(ComParams params) {
        try {
            String param = (String) params.getBaseParameter(UniversalSourceFromParameter.KEY_PARAM);
            JsonBuilder jb = new JsonBuilder();
            jb.object();
            JSONObject object = new JSONObject(param);
            if (object.has(KEY_SRC_FROM)) {
                jb.putStringValue(KEY_SRC_FROM, object.getString(KEY_SRC_FROM));
            }
            jb.endObject();
            return jb.getJson();
        } catch (JSONException e) {
            e.printStackTrace();
            return "";
        }

    }

    public String getResId() {
        return  ComAPIManager.getComAPIManager().getSystemAPI().getResId();
    }

    public void setQid(String qid) {
        mQid = qid;
    }

    public String getQid() {
        return mQid;
    }

    public void setSrcFrom(String srcFrom) {
        mSrcFrom = srcFrom;
    }

    public String getSrcFrom() {
        return mSrcFrom;
    }

    public void setLdata(String ldata) {
        mLdata = ldata;
        try {
            JSONObject obj = new JSONObject(ldata);
            String srcFrom = obj.getString(KEY_SRC_FROM);
            setSrcFrom(srcFrom);
        } catch (JSONException e) {
            LogUtils.d(TAG, "JSONException e.message = " + e.getMessage());
            e.printStackTrace();
            setSrcFrom("");
        }
    }

    public String getLdata() {
        return mLdata;
    }

    public void cleanQid() {
        mQid = "";
    }

    public void clear() {
        mQid = "";
        mLdata = "";
        mSrcFrom = "";
    }

    public static String getStatisticKeyValues() {
        return "&resid="+ UniversalSourceFromParameter.getInstance().getResId()
                + "&qid=" + UniversalSourceFromParameter.getInstance().getQid()
                + "&src_from=" + UniversalSourceFromParameter.getInstance().getSrcFrom();
    }

    @Override
    public String toString() {
        return "resid = " + getResId() + " qid = " + getQid() + " src_from = " + getSrcFrom();
    }
}
